package com.wgts.wgts.mvp.view.activity.shopkeeper;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.control.recycleview.BaseLoadMoreAdapter;
import com.wgts.wgts.netmodle.resp.WgtsKeepAssetEntity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AssetDetailAdapter extends BaseLoadMoreAdapter<AssetDetailAdapter.DetailViewHolder> {



    private Context context;


    private List<WgtsKeepAssetEntity> list;

    public AssetDetailAdapter(Context context, List<WgtsKeepAssetEntity> list) {
        super(context);
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public DetailViewHolder onCreateBodyViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new DetailViewHolder(LayoutInflater.from(context).inflate(R.layout.asset_detail_view_holder, viewGroup, false));
    }

    @Override
    public void onBindBodyViewHolder(@NonNull DetailViewHolder detailViewHolder, int i) {

//        String temp = list.get(i).desc;
//
//        String temp1 = list.get(i).orderCode;
//
//        if(list.get(i).orderCode.equals("")){
//            detailViewHolder.tvDetail.setText(list.get(i).desc);
//        }else{
//            if(list.get(i).desc.contains(list.get(i).orderCode)){
//
//                //设置部分文字点击事件
//                ClickableSpan MyClickableSpan = new ClickableSpan() {
//                    @Override
//                    public void onClick(View widget) {
//                        Log.d("", "");
//                    }
//
//                    public void updateDrawState(TextPaint ds) {
//                        ds.setColor(ContextCompat.getColor(context, R.color.bulue_copy));
//                    }
//                };
//
//                int tmp = list.get(i).orderCode.length();
//                //字符串可点击
//                SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(list.get(i).desc);
//                spannableStringBuilder.setSpan(MyClickableSpan,  list.get(i).desc.indexOf(list.get(i).orderCode), list.get(i).orderCode.length() + list.get(i).desc.indexOf(list.get(i).orderCode), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//                detailViewHolder.tvDetail.setText(spannableStringBuilder);
//            }else {
//                detailViewHolder.tvDetail.setText(list.get(i).desc);
//            }
//        }

        detailViewHolder.textTitle.setText(list.get(i).getTitle());

        detailViewHolder.tvDetail.setText(list.get(i).getContent());



        switch (list.get(i).getType()){
            case "INCOME":
                detailViewHolder.tvMoney.setText("+" +  list.get(i).getMoney() + "元" );
                break;
            case "EXPENSE":
                detailViewHolder.tvMoney.setText("-" +  list.get(i).getMoney() + "元");
                break;
            case "EXPENSE_FREEZE":
                detailViewHolder.tvMoney.setText("-" +  list.get(i).getMoney() + "元(审核中)");
                break;
            case "EXPENSE_REJECT":
                detailViewHolder.tvMoney.setText("+" +  list.get(i).getMoney() + "元（拒绝）");
                break;

        }
        detailViewHolder.tvTime.setText(list.get(i).getTime());
        detailViewHolder.tvDate.setText(list.get(i).getDate());
    }

    @Override
    public int getBodyItemCount() {
        return list.size();
    }

    @Override
    public int getBodyItemViewType(int position) {
        return 0;
    }

    public class DetailViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_title)
        TextView textTitle;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.linear_time)
        LinearLayout linearTime;
        @BindView(R.id.tv_money)
        TextView tvMoney;
        @BindView(R.id.tv_detail)
        TextView tvDetail;
        @BindView(R.id.tv_date)
        TextView tvDate;

        public DetailViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * 这个类 实际上和第一种改变颜色的方法差不多，只不过 那是个专门改变颜色的Span，这是个专门负责点击处理的Span
     * @author Administrator
     */
    class MyClickableSpan extends ClickableSpan{
        private Context context;

        public MyClickableSpan(Context context)
        {
            this.context = context;
        }

        //在这里设置字体的大小，等待各种属性
        public void updateDrawState(TextPaint ds) {
            ds.setColor(ContextCompat.getColor(context, R.color.bulue_copy));
        }

        @Override
        public void onClick(View widget) {
            //Intent intent = new Intent(MainActivity.this,OtherActivity.class);
            //startActivity(intent);
        }
    }

}
